<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */

namespace Modules\Admin\Http\Controllers;

use Illuminate\Support\Facades\Request;
use Modules\Admin\Service\OrderService;
use Excel;

class OrderController extends BaseController
{
    /**
     *  获取积分订单列表（积分）
     *  hinq 2019 09 18
     *
     ***/
    public function index_integral()
    {
        $arr_search = (array)Request::all();
        $res = OrderService::get_index_integral((array)$arr_search);
        echo_error($res);
        return view('admin::order.integral.index', $res['data']);
    }

    /**
     *  订单详情
     *  hinq 2019 09 18
     *
     **/
    public function order_details()
    {
        $nId = (int)Request::get('id', 0);
        $info = OrderService::order_details((int)$nId);
        return view('admin::order.integral.detail', ['info' => $info['data']]);
    }

    /**
     *  订单处理
     *
     **/
    public function changStatus()
    {
        $data = Request::all();

        shiwu_start();
        $res = OrderService::changStatus($data);
        shiwu_end($res);

        return $res;
    }


    /**
     * 快速发货
     */
    public function deliverGoods()
    {
        $data = Request::all();

        shiwu_start();
        $res = OrderService::deliverGoods($data);
        shiwu_end($res);

        return $res;
    }

    /**
     * 删除
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete()
    {
        $n_id = (int)Request::get('id', 0);
        return OrderService::delete_one($n_id);
    }


    /**
     * 导出Excel
     */
    public function export()
    {
        $arr_search = (array)Request::all();
        $list = OrderService::get_order_list($arr_search);
        $orderList = [];
        foreach ($list as $key => $value) {
            $orderList[$value['order_status']][] = $value;
        }

        $cellLetter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $title = "积分订单" . date('Y-m-d h:i');
        Excel::create($title, function ($excel) use ($orderList, $cellLetter) {
            foreach ($orderList as $sheet_name => $item) {
                $cellData = [];
                $cellData[0] = ['订单编号', '订单状态', '收货人', '联系方式', '收货地址', '商品名称【规格】', '订单总积分', '下单时间', '快递公司', '快递单号', '发货时间', '收货时间'/*, '买家留言'*/];

                foreach ($item as $key => $value) {
                    $user_name = $value['get_address_info']['username'];
                    $user_phone = $value['get_address_info']['phone'];
                    $user_address = $value['get_address_info']['province'] . $value['get_address_info']['city'] . $value['get_address_info']['area'] . $value['get_address_info']['address'];
                    $express = $value['get_express_info']['name'];
                    $goods = $value['get_goods_info']['name'] . '【' . $value['product_info']['specs']['name'] . '】';
                    $cellData[$key + 1] = [
                        $value['order_no'],       //订单编号
                        $value['order_status'],   //订单状态
                        $user_name, $user_phone, $user_address,
                        $goods,                   //商品
                        $value['order_integral'], //订单总积分
                        $value['created_at'],     //下单时间
                        $express,                 //快递公司
                        $value['delivery_no'],    //快递单号
                        $value['delivery_time'],  //发货时间
                        $value['receiving_time'], //收货时间
                        //$value['remark'],         //买家留言
                    ];
                }

                $excel->sheet($sheet_name, function ($sheet) use ($cellData, $cellLetter) {
                    $sheet->rows($cellData);
                    $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
                    $sheet->getStyle('A1:L' . count($cellData))->getAlignment()->setVertical('center');//垂直居中
                    $sheet->getStyle('A1:L1')->applyFromArray([
                        'font' => ['bold' => true, 'color' => ['rgb' => '0072ff',]]//字体设置
                    ]);
                    $sheet->getStyle('A1:L' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'center'],
                    ]);
                    $sheet->getStyle('E2:F' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'left'],
                    ]);

                    for ($i = 0; $i <= count($cellData[0]); $i++) {
                        $cell = $cellLetter[$i];
                        if (in_array($cell, ['A', 'E'])) {
                            $width = 25;
                        } elseif (in_array($cell, ['B', 'C', 'D', 'G'])) {
                            $width = 15;
                        } elseif (in_array($cell, ['E', 'F'])) {
                            $width = 50;
                        } else {
                            $width = 20;
                        }
                        $sheet->setWidth($cell, $width);
                    }
                });

                $excel->setactivesheetindex(0);//设置默认打开第一个sheet
            }
        })->export('xls');
    }

}